// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command

/**
 * 更新申请或线索
 */
exports.main = async (event, context) => {
  const { id, operation } = event

  if (operation === 'accept') {
    // 接受申请或采纳线索
    return await db.collection('request')
      .doc(id)
      .update({
        data: {
          status: 1,
          tosee: 1
        }
      })
  } else if (operation === 'reject') {
    // 拒绝申请或不采纳线索
    return await db.collection('request')
      .doc(id)
      .update({
        data: {
          status: 2,
          tosee: 1
        }
      })
  } else if (operation === 'see') {
    // 更新申请或线索的待查看状态
    return await db.collection('request')
      .doc(id)
      .update({
        data: {
          tosee: 0
        }
      })
  }
}